import {
    BASE_URL,
    TOKEN
} from './config.js'
import MD5 from "../utils/md5"


export const request = obj => {
    let time = new Date().getTime()+5000
    let data = obj.data || {}



    data.t = time
    data.siteid = 2
    // data.spreadid = 5

    // data.agentid = 2

    return new Promise((resolve, reject) => {
        $device.getInfo({
            success(e) {
                data.device = e.brand
                getStorage('uid', (r) => {
                    data.uid = r || ''
                    getStorage("spreadid", r1 => {
                        data.spreadid = !r1 || r1 == 0 ? 0 : r1
                        getStorage('agentid', r2 => {
                            data.agentid = !r2 || r2 == 0 ? 2 : r2
                            let qian = objKeysSort(data)
                            data._ = qian
                            $fetch.fetch({
                                url: BASE_URL + obj.url,
                                data: data,
                                header: {
                                    Token: TOKEN
                                },
                                method: obj.method,
                                success(res) {
                                    let a = JSON.parse(res.data)
                                    if (a.code == 601 || a.code == 102) {
                                        $utils.showToast(a.msg)
                                        setTimeout(()=>{
                                            $router.push({
                                                uri: "pages/login"
                                            })
                                        },1000)
                                        return;
                                    } else {
                                        resolve(JSON.parse(res.data))

                                    }
                                },
                                fail(re) {
                                    $utils.showToast("网络有问题,请稍后重试")
                                }

                            })
                        })
                    })
                })
            }
        })
    })
}


function getStorage(key, func) {

    $storage.get({
        key: key,
        success(r) {
            func(r)
        }
    })
}
// 排序函数获取签名
function objKeysSort(obj) {
    /**
     * 先用Object内置类的keys方法获取要排序对象的属性名
     * 再利用Array原型上的sort方法对获取的属性名进行排序
     * newKey是一个数组
     */
    delete obj._
    var newKey = Object.keys(obj).sort();
    let index = newKey.length
    // 创建一个新对象，用于存放排好序的键值对
    var newObj = {};
    var str = ""
    // 遍历数组.
    for (var i = 0; i < newKey.length; i++) {
        // 向新创建的对象中按照排好的顺序依次增加键值对
        newObj[newKey[i]] = obj[newKey[i]];
        str = str + newKey[i] + "=" + obj[newKey[i]] + "&"
    }
    str = str + "privateKey=" + TOKEN
    //转化md5
    let a = MD5(str)
    //转大写
    let b = a.toUpperCase()

    //切割签名
    let qian = b.substring(index + 1, index + 17)
    // 返回新对象
    return qian;
}



